package com.coinworld.utils;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;


public class SMSUtil {

    static final Logger logger = LoggerFactory.getLogger(SMSUtil.class);
    private final String BASE_URI = "https://sdk2.028lk.com/sdk2/BatchSend2.aspx";
    private static String CorpID = "CDJS009112";
    private static String Pwd = "zm0513@";
    private static String Cell = "";//扩展号
    private static String SendTime = "";//发送时间

    public boolean sendSMS(String Mobile, String code) {
        boolean flag = true;
        try {
            String Content = "您的验证码为" + code + "如非本人操作请忽略!";
            Map<String, String> paramMap = new HashMap<String, String>(10);
            paramMap.put("CorpID", CorpID);
            paramMap.put("Pwd", Pwd);
            paramMap.put("Mobile", Mobile);
            paramMap.put("Content", Content);
            paramMap.put("Cell", Cell);
            paramMap.put("SendTime", SendTime);
            HttpAsyncUtils.send(BASE_URI, paramMap, "GB2312");
            logger.info("send sms success");
        }catch (Exception e) {
            flag = false;
        }
       return flag;
    }

    private static final String regionId = "cn-hangzhou";
    private static final String accessKeyId = "LTAI4Fv8wj2gQBtfvxKitkJe";
    private static final String secret = "UzMim42QOlOq7uonxpEATCjTPz7SiO";
    private static final String SMSTemplateCode = "SMS_177543346";
    private static final String signName = "蜂旅科技";
    private static final String domain = "dysmsapi.aliyuncs.com";
    private static final String codeOk = "OK";

    public static boolean sendAliYunSMS(String mobile,String code){
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(domain);
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", SMSTemplateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject result = JSONObject.parseObject(response.getData());
            if (!codeOk.equals(result.getString("Code"))){
                logger.error("sendAliYunSMS error mobile:{} msg:{}",mobile,response.getData());
                return false;
            }
        } catch (ClientException e) {
            logger.error("sendAliYunSMS error mobile:{},msg:{}",mobile,e);
            e.printStackTrace();
            return false;
        }
        return true;
    }




    /**
     * 生成验证码
     */
    public static String createCode() {
        Random random = new Random();
        String result = "";
        for (int i = 0; i < 6; i++) {
            result += random.nextInt(10);
        }
        return result;
    }

}
